/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.manager;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edi.Kola;
import com.edi.client.plugin.Plugin;
import com.edi.client.plugin.PluginClassLoader;
import com.edi.manager.monitor.ScriptPluginMonitor;
import com.edi.manager.monitor.StandardPluginMonitor;

/**
 * 插件管理器
 * 
 * 
 */
public class PluginManager {
	private static final Log log = LogFactory.getLog(PluginManager.class);

	private ScriptPluginMonitor scriptPluginMonitor;

	private ScheduledExecutorService executor;

	private Map<String, Plugin> scriptPlugins;

	private Map<String, Plugin> standardPlugins; //以jar包形式存在的插件

	private Map<String, PluginClassLoader> classLoaders;

	private StandardPluginMonitor jarPluginMonitor;

	private ScheduledExecutorService jarExecutor;

	public PluginManager() {
		scriptPlugins = new HashMap<String, Plugin>();
		classLoaders = new HashMap<String, PluginClassLoader>();
		standardPlugins = new HashMap<String, Plugin>();
		init();
	}

	public void init() {
		scriptPluginMonitor = new ScriptPluginMonitor(this);
		//		scriptPluginMonitor.firstRun();
		executor = new ScheduledThreadPoolExecutor(1);
		executor.scheduleWithFixedDelay(scriptPluginMonitor, 0, 60,
				TimeUnit.SECONDS);

		//初始化jar插件
		jarPluginMonitor = new StandardPluginMonitor(this, Kola
				.getServiceManager());
		//		jarPluginMonitor.firstRun();
		jarExecutor = new ScheduledThreadPoolExecutor(1);
		jarExecutor.scheduleWithFixedDelay(jarPluginMonitor, 0, 60,
				TimeUnit.SECONDS);

	}

	public Plugin getScriptPluginByName(String pluginName) {
		return scriptPlugins.get(pluginName);
	}

	public void addScriptPlugin(Plugin plugin) {
		this.scriptPlugins.put(plugin.getName(), plugin);
	}

	public void removeScriptPlugin(String pluginName) {
		synchronized (scriptPlugins) {
			scriptPlugins.remove(pluginName);
		}
	}

	public boolean containsPlugin(String name) {
		for (String key : this.scriptPlugins.keySet()) {
			if (key.equalsIgnoreCase(name))
				return true;
		}
		return false;
	}

	public boolean containsPluginClassLoader(String jarName) {
		for (String key : this.classLoaders.keySet()) {
			if (key.equalsIgnoreCase(jarName))
				return true;
		}
		return false;
	}

	public Map<String, PluginClassLoader> getClassLoaders() {
		return classLoaders;
	}

	public PluginClassLoader loadClassLoader(String jarName) {
		return this.classLoaders.get(jarName);
	}

	public void setClassLoaders(Map<String, PluginClassLoader> classLoaders) {
		this.classLoaders = classLoaders;
	}

	public void addClassLoader(String plugin, PluginClassLoader classLoader) {
		this.classLoaders.put(plugin, classLoader);
	}

	public void removeClassLoader(String plugin) {
		synchronized (classLoaders) {
			this.classLoaders.remove(plugin);
		}
	}

	public Map<String, Plugin> getScriptPlugins() {
		return scriptPlugins;
	}

	public void setScriptPlugins(Map<String, Plugin> scriptPlugins) {
		this.scriptPlugins = scriptPlugins;
	}

	public Map<String, Plugin> getStandardPlugins() {
		return standardPlugins;
	}

	public Plugin getStandardPlugin(String name) {
		return standardPlugins.get(name);
	}

	public void setStandardPlugins(Map<String, Plugin> standardPlugins) {
		this.standardPlugins = standardPlugins;
	}

	public boolean containsStandardPlugin(String name) {
		for (String key : this.standardPlugins.keySet()) {
			if (key.equalsIgnoreCase(name))
				return true;
		}
		return false;
	}

	public void removeStandardPlugin(String name) {
		this.standardPlugins.remove(name);
	}

	public void addStandardPlugin(String name, Plugin plugin) {
		this.standardPlugins.put(name, plugin);
	}

}
